Merge pull request #184 from ms32035/master

Postgresql compatibility for agent model

Andrew Cantino 10 anni fa
parent
commit
0e7c5c5b95
3 ha cambiato i file con 18 aggiunte e 3 eliminazioni
  1. 4 3
      app/models/agent.rb
  2. 1 0
      config/database.yml
  3. 13 0
      lib/rdbms_functions.rb

+ 4 - 3
app/models/agent.rb

@@ -10,6 +10,7 @@ class Agent < ActiveRecord::Base
10 10
   include AssignableTypes
11 11
   include MarkdownClassAttributes
12 12
   include JSONSerializedField
13
+  include RDBMSFunctions
13 14
 
14 15
   markdown_class_attributes :description, :event_description
15 16
 
@@ -127,7 +128,7 @@ class Agent < ActiveRecord::Base
127 128
     if keep_events_for == 0
128 129
       events.update_all :expires_at => nil
129 130
     else
130
-      events.update_all "expires_at = DATE_ADD(`created_at`, INTERVAL #{keep_events_for.to_i} DAY)"
131
+      events.update_all "expires_at = " + rdbms_date_add("created_at", "DAY", keep_events_for.to_i) 
131 132
     end
132 133
   end
133 134
 
@@ -265,8 +266,8 @@ class Agent < ActiveRecord::Base
265 266
 
266 267
         agents_to_events = {}
267 268
         Agent.connection.select_rows(sql).each do |receiver_agent_id, source_agent_id, event_id|
268
-          agents_to_events[receiver_agent_id] ||= []
269
-          agents_to_events[receiver_agent_id] << event_id
269
+          agents_to_events[receiver_agent_id.to_i] ||= []
270
+          agents_to_events[receiver_agent_id.to_i] << event_id
270 271
         end
271 272
 
272 273
         event_ids = agents_to_events.values.flatten.uniq.compact

+ 1 - 0
config/database.yml

@@ -21,6 +21,7 @@ test:
21 21
   socket: <%= ENV['DATABASE_SOCKET'] || ["/var/run/mysqld/mysqld.sock", "/opt/local/var/run/mysql5/mysqld.sock", "/tmp/mysql.sock"].find{ |path| File.exist? path } %>
22 22
   encoding: <%= ENV['DATABASE_ENCODING'] || "utf8" %>
23 23
   reconnect: <%= ENV['DATABASE_RECONNECT'] || "true" %>
24
+  port: <%= ENV['DATABASE_PORT'] || "" %>
24 25
   pool: <%= ENV['DATABASE_POOL'] || "5" %>
25 26
 
26 27
 production:

+ 13 - 0
lib/rdbms_functions.rb

@@ -0,0 +1,13 @@
1
+module RDBMSFunctions
2
+  def rdbms_date_add(source, unit, amount)
3
+    adapter_type = connection.adapter_name.downcase.to_sym
4
+    case adapter_type
5
+      when :mysql, :mysql2
6
+        "DATE_ADD(`#{source}`, INTERVAL #{amount} #{unit})"
7
+      when :postgresql    
8
+        "(#{source} + INTERVAL '#{amount} #{unit}')"
9
+      else
10
+        raise NotImplementedError, "Unknown adapter type '#{adapter_type}'"
11
+    end
12
+  end
13
+end